<template>
  <div>
    <!-- <el-card> 到货验收 </el-card> -->
    <el-card>
      <div class="list-h">
        <el-button
          type="primary"
          @click="handleAdd"
          v-auth="`/acceptance/concealed--add`"
          >新增</el-button
        >
        <el-form
          :inline="true"
          :model="formInline"
          ref="formRef"
          class="demo-form-inline"
        >
          <el-form-item label="单据编号" prop="billNumLike">
            <el-input
              class="width-custom120"
              v-model="formInline.billNumLike"
            ></el-input>
          </el-form-item>
          <el-form-item label="单据名称" prop="billNameLike">
            <el-input
              class="width-custom120"
              v-model="formInline.billNameLike"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目名称" prop="projectNameLike">
            <el-input
              class="width-custom120"
              v-model="formInline.projectNameLike"
            ></el-input>
          </el-form-item>
          <el-form-item label="合同名称" prop="contractNameLike">
            <el-input
              class="width-custom120"
              v-model="formInline.contractNameLike"
            ></el-input>
          </el-form-item>
          <el-form-item label="创建人" prop="createUserNameLike">
            <!-- <el-select clearable class="width-custom120"> </el-select> -->
            <el-input
              class="width-custom120"
              v-model="formInline.createUserNameLike"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="flowStatus">
            <el-select
              clearable
              class="width-custom120"
              v-model="formInline.flowStatus"
            >
              <el-option
                v-for="item in flowStatusOption"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              class="width-custom220"
              v-model="timeValue"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="handleChangeDate"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleSearch">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-skeleton style="height: 50vh" :rows="8" :loading="loading" animated>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        >
          <template #empty>
            <el-empty :image-size="150" description="暂无数据"></el-empty>
          </template>
          <el-table-column type="index" label="序号">
            <template v-slot="scope">
              <span>{{
                (formInline.pageNum - 1) * formInline.pageSize +
                scope.$index +
                1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="projectName" label="项目名称">
          </el-table-column>
          <el-table-column prop="billNum" label="单据编号" width="180">
          </el-table-column>
          <el-table-column prop="billName" label="单据名称"> </el-table-column>
          <el-table-column prop="contractName" label="合同名称">
          </el-table-column>
          <el-table-column prop="contractNum" label="合同编号">
          </el-table-column>
          <el-table-column prop="partyA" label="甲方单位"> </el-table-column>
          <el-table-column
            prop="directorAName"
            label="甲方负责人"
            min-width="100"
          >
          </el-table-column>
          <el-table-column prop="partyB" label="乙方单位"> </el-table-column>
          <el-table-column
            prop="directorBName"
            label="乙方负责人"
            min-width="100"
          >
          </el-table-column>
          <el-table-column prop="createUserName" label="创建人">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="140">
          </el-table-column>
          <el-table-column prop="flowStatus" label="状态">
            <template #default="{ row }">
              <div :class="formatterStatusColor(row)"></div>
              {{ formatterStatusType(row) }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="{ row }">
              <el-button
                type="text"
                size="small"
                @click="handleDetail(row)"
                v-auth="`/acceptance/concealed--look`"
                >查看</el-button
              >
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除该条记录吗？"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button
                    type="text"
                    v-auth="`/acceptance/concealed--delete`"
                    v-showbtn="`${row.createUser}`"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <re-pagination
          v-if="total > 0"
          :total="total"
          v-model:page="formInline.pageNum"
          v-model:limit="formInline.pageSize"
          @pagination="handleSearch"
        />
      </el-skeleton>
    </el-card>
    <el-dialog title="选择隐蔽验收类型" v-model="dialogVisible" width="400px">
      <div class="dialog__row">
        <el-button type="primary" @click="handleToDetail(5)"
          >涉及监理验收</el-button
        >
      </div>
      <div class="dialog__row">
        <el-button type="primary" @click="handleToDetail(6)"
          >不涉及监理验收</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";

import { postComFlowList, postComFlowDelete } from "/@/api/acceptance";
import { FlowStatusEnum } from "./types";
import RePagination from "/@/components/RePagination/index.vue";
import NotifMessage from "/@/utils/message/notification";

const formRef = ref<HTMLFormElement | null>(null) as any;
const tableData = ref([]);
const router = useRouter();
let loading = ref<boolean>(false);
let dialogVisible = ref<boolean>(false);
let formInline = reactive({
  billNumLike: "",
  billNameLike: "",
  projectNameLike: "",
  contractNameLike: "",
  createUserNameLike: "",
  flowStatus: "",
  pageNum: 1,
  pageSize: 10,
  flowMaxType: "CNCB", // CMPB 竣工   STAB 开工验收
  createTimeStrStart: "",
  createTimeStrEnd: ""
});

const flowStatusOption = FlowStatusEnum;

let total = ref<number>(0);
let timeValue = ref(null);

// 格式化类型
const formatterStatusType = row => {
  return FlowStatusEnum[row.flowStatus].label;
};
const formatterStatusColor = row => {
  return {
    redColor: row.flowStatus === 0,
    greenColor: row.flowStatus === 1,
    grayColor: row.flowStatus === 2
  };
};

const getTableList = async (obj: object = {}) => {
  try {
    loading.value = true;
    const { code, data } = await postComFlowList(obj);
    if (code === 200) {
      tableData.value = data.list;
      total.value = data.rows;
    }
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
};

const handleChangeDate = item => {
  console.log(item, "date");
  if (item) {
    formInline.createTimeStrStart = item[0];
    formInline.createTimeStrEnd = item[1];
  } else {
    formInline.createTimeStrStart = "";
    formInline.createTimeStrEnd = "";
  }
};

const handleReset = () => {
  formRef.value.resetFields();
  timeValue.value = null;
  handleChangeDate(null);
};

const handleSearch = () => {
  getTableList(formInline);
};

const handleAdd = () => {
  dialogVisible.value = true;
};

const handleToDetail = (code: number): void => {
  router.replace({
    path: "/projectsA/acceptance/concealed/detail",
    query: { mode: "add", nodeDefId: "ACT_010", flowMinType: code }
  });
};

const handleDetail = row => {
  const { id, flowId, nodeDefId, instanceId, preNodeId, flowMinType } = row;
  router.replace({
    path: "/projectsA/acceptance/concealed/detail",
    query: {
      mode: "read",
      contractId: id,
      flowId,
      nodeDefId: instanceId ? nodeDefId.toUpperCase() : "ACT_010",
      instanceId,
      preNodeId,
      flowMinType
    }
  });
};

const handleDelete = async row => {
  try {
    const params = {
      id: row.id
    };
    const { code, message } = await postComFlowDelete(params);
    if (code === 200) {
      NotifMessage.success(message);
      getTableList(formInline);
    } else {
      NotifMessage.error(message);
    }
  } catch (err) {
    console.log(err);
  }
};

getTableList(formInline);
</script>
<style scoped lang="scss">
.redColor {
  width: 10px;
  height: 10px;
  background: #f5a623;
  border-radius: 50%;
  display: inline-block;
}
.greenColor {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #21d371;
  display: inline-block;
}
.grayColor {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #979797;
  display: inline-block;
}

.dialog__row {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}
</style>
